<template>
  <el-container class="home_container">
    <!--    头部区域-->
    <el-header class="home_header">
      <el-container class="home_header_left">
        <img src="../assets/heima.png"/>
        <span>后台管理系统</span>
      </el-container>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--    内容区域-->
    <el-container>
      <!--      左侧区域-->
      <el-aside class="home_aside" :width="taggle ? '200px':'64px'">
        <div class="home_aside_toggle" @click="onTaggleClick">|||</div>
        <el-menu
          unique-opened
          background-color="#313743"
          text-color="#fff"
          active-text-color="#3898FD"
          router
          :default-active="activePath"
          :collapse="!taggle"
          :collapse-transition="false">
          <!--          一级标题-->
          <el-submenu :index="'/' + item.path" v-for="item in menuList" v-bind:key="item.id">
            <template slot="title">
              <i class="home_aside_menu_icon" :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!--            二级标题-->
            <el-menu-item :index="'/' + childItem.path" @click="onSubMenuClick('/' + childItem.path)"
                          v-for="childItem in item.children" v-bind:key="childItem.id">
              <i class="el-icon-menu"></i>
              <span>{{ childItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--      右侧区域-->
      <el-main class="home_main">
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      taggle: true,
      activePath: '',
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout: function () {
      window.sessionStorage.clear()
      this.$message.success('退出成功')
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      } else {
        this.menuList = res.data
      }
    },
    onTaggleClick () {
      this.taggle = !this.taggle
    },
    onSubMenuClick (activePath) {
      this.activePath = activePath
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}

.home_header {
  background-color: #363340;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  color: white;
  font-size: 20px;
  padding-left: 0;
}

.home_header_left {
  display: flex;
  align-items: center;

  span {
    margin-left: 10px;
  }
}

.home_aside {
  background-color: #313743;

  .home_aside_toggle {
    height: 30px;
    line-height: 30px;
    font-size: 15px;
    color: white;
    text-align: center;
    background-color: #666666;
    cursor: pointer;
  }

  .el-menu {
    border-right: none;
  }

  .home_aside_menu_icon {
    margin-right: 10px;
  }
}

.home_main {
  background-color: #E9F7F1;
}
</style>
